package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcAirplane;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.util.JQPage;

public class AirManagerController extends Controller {
	public void index() {
		this.render("/air/air.html");
	}

	public void queryAir() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		StringBuffer sb = new StringBuffer();
		String code = this.getPara("code");
		String name = this.getPara("name");
		String org_id = this.getPara("org_id");
		//查询用户组织的所有子组织
		StringBuffer queryOrgSql = new StringBuffer();
		queryOrgSql.append("select o.org_id from idc_org o where o.parent_id = ? ");
		queryOrgSql.append(" union ");
		queryOrgSql.append("select oo.org_id from idc_org oo where oo.parent_id in ( ");
		queryOrgSql.append("select ooo.org_id from idc_org ooo where ooo.parent_id = ? ");
		queryOrgSql.append(") order by org_id ");
		List<Integer> orgList = Db.query(queryOrgSql.toString() , new Object[]{u.getOrgId(), u.getOrgId()});
		
		orgList.add(u.getOrgId());
		Collections.sort(orgList);
		
		sb.append("select a.plane_id , a.code, a.name, a.org_id, o.name as org_name, ");
		sb.append("a.create_user_id,  u.username as create_user_name, ");
		sb.append("a.create_time, a.modify_time, a.memo ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_airplane a left join idc_org o on a.org_id = o.org_id ");
		sqlFrom.append("left join idc_user u on a.create_user_id = u.user_id where 1=1 ");
		if(code !=null ){
			sqlFrom.append(" and a.code like '%" + code + "%' ");
		}
		
		if(name !=null ){
			sqlFrom.append(" and a.name like '%" + name + "%' ");
		}
		
		if(org_id !=null ){
			sqlFrom.append(" and a.org_id = " +  org_id );
		}
		
		sqlFrom.append(" and a.org_id in (  ");
		Object[] param = orgList.toArray();
		for(int i=0; i<param.length; i++){
			if(i==param.length-1){
				sqlFrom.append(" ?) ");
			}else{
				sqlFrom.append(" ?, ");
			}
		}
		
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString(), param);
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}

	public void add() {
		IdcAirplane idcAirplane = new IdcAirplane();
		String code = this.getPara("code");
		String name = this.getPara("name");
		String org_id = this.getPara("org_id");
		String memo = this.getPara("memo");
		Date now = new Date();
		IdcUser u = this.getSessionAttr("SYS_USER");
		List<IdcAirplane> airList = idcAirplane.find("select * from idc_airplane a where a.code = ?", new Object[]{code});
		Map resultMap = new HashMap();
		if(airList.size()>0){
			resultMap.put("result", 0);
			resultMap.put("error_code", 1000);
			resultMap.put("error_desc", "机尾号为："  + code + " 的飞机已存在，不允许存在相同机尾号的飞机！新增失败！");
			this.renderJson(resultMap);
		}else{
			String sql = "insert into idc_airplane (code, name, org_id, memo, create_time , create_user_id) values(?,?,?,?,?,?)";
			int  result = Db.update(sql, new Object[]{code, name, org_id, memo, now, u.getUserId()});
			resultMap.put("result", result);
			this.renderJson(resultMap);
		}
		
	}

	public void delete() {
		final String planeIds = this.getPara("planeIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] planeIdArr = planeIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_airplane where plane_id in(");
				for(int i=0; i<planeIdArr.length; i++){
					if(i==planeIdArr.length-1){
						sb.append(planeIdArr[i] + ")");
					}else{
						sb.append(planeIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==planeIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		IdcAirplane idcAirplane = new IdcAirplane();
		int planeId = this.getParaToInt("planeId");
		String name = this.getPara("name");
		int org_id = this.getParaToInt("org_id");
		String memo = this.getPara("memo");
		Date now = new Date();
		IdcUser u = this.getSessionAttr("SYS_USER");
		idcAirplane.setPlaneId(planeId);
		idcAirplane.setName(name);
		idcAirplane.setOrgId(org_id);
		idcAirplane.setMemo(memo);
		idcAirplane.setModifyTime(now);
		boolean  result = idcAirplane.update();
		this.renderJson(result);
	}
	
}
